import {guid} from "@/utils";
import store from '@/store'


const requestStatusMixin = {
  data() {
    return {
      requestKey: guid(), // 每个按钮的唯一标识符
    };
  },
  computed: {
    isRequesting() {
      return this.getRequestStatus(this.requestKey);
    },
  },
  methods: {
    setRequestStatus(config) {
      console.log(config.requestKey)
      if (this.getRequestStatus(config.requestKey)) {
        return;
      } else {
        const requestKey = config.requestKey;
        store.dispatch('requests/setRequestStatus', { key: requestKey, value: true });
        return config;
      }
    },
    updateRequestStatus(response) {
      console.log(response.config,"response")
      const requestKey = response.config.requestKey;
      store.dispatch('requests/setRequestStatus', { key: requestKey, value: false });
      return response;
    },
    handleRequestError(error) {
      const requestKey = error.config.requestKey;
      store.dispatch('requests/setRequestStatus', { key: requestKey, value: false });
      return Promise.reject(error);
    },
    getRequestStatus(requestKey) {
      return store.state.requests.requestStatus[requestKey];
    },
  },
  /*created() {
    this.setRequestStatus = this.setRequestStatus.bind(this);
    this.updateRequestStatus = this.updateRequestStatus.bind(this);
    this.handleRequestError = this.handleRequestError.bind(this);
    this.getRequestStatus = this.getRequestStatus.bind(this);
  },*/
};

export default requestStatusMixin;
